const axios = require('axios').default;
const cheerio = require('cheerio');
const fs = require('fs');

const jobs = [];
let pages = 5;

function generateProxyIp() {
  const ip =
    // "http://" +
    Math.floor(Math.random() * 255) +
    '.' +
    Math.floor(Math.random() * 255) +
    '.' +
    Math.floor(Math.random() * 255) +
    '.' +
    Math.floor(Math.random() * 255);

  const port = Math.floor(Math.random() * 65535);
  return {
    ip,
    port,
  };
}

// const { cities } = require("./utils/city");
// const reqJobs = [];
// cities.zpData.cityList.forEach((item) => {
//   const proxy = generateProxyIp();
//   for (let i = 1; i < pages; i++) {
//     reqJobs.push(
//       axios.get(
//         //`https://www.zhipin.com/wapi/zpgeek/mobile/search/joblist.json?city=${item.code}&querySource=1&page=${i}&city=&query=`,
//         `https://www.zhipin.com/wapi/zpgeek/miniapp/search/joblist.json?pageSize=15&query=java&city=101280100&source=1&sortType=0&subwayLineId=&subwayStationId=&districtCode=&businessCode=&longitude=&latitude=&position=&expectId=&expectPosition=&page=2&appId=10002`,
//         {
//           headers: {
//             Host: `www.zhipin.com`,
//             Connection: `keep-alive`,
//             "content-type": `application/x-www-form-urlencoded`,
//             zpAppId: `10002`,
//             ua: `{"model":"iPhone 12 Pro<iPhone13,3>","platform":"ios"}`,
//             platform: `zhipin/windows`,
//             ver: `5.1502`,
//             scene: `1089`,
//             "X-Forwarded-For": proxy.ip,
//             miniappVersion: `5.1502`,
//             wt2: `D8jgSLlWKuMMFtxmMI7X2dNtlAuOvJ7ESzMMoPayZbPaMKFSk9FARpNjYk65UUe9krL3QB47cGM0raDkEMtD_sg~~`,
//             mpt: `f4b5cea4f5248538d7e8f27f00e01ae1`,
//             "x-requested-with": `XMLHttpRequest`,
//             "Accept-Encoding": `gzip,compress,br,deflate`,
//             "User-Agent": `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF XWEB/8287`,
//             // "User-Agent": `Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.40(0x1800282c) NetType/WIFI Language/zh_CN`,
//             Referer: `https://servicewechat.com/wxa8da525af05281f3/461/page-frame.html`,
//           },
//         }
//       )
//     );
//   }
// });

// Promise.all(reqJobs).then((data) => {
//   // console.log(data);
//   data.forEach((item) => {
//     console.log(item.data);
//     // const $ = cheerio.load(item.data.zpData.html);
//     // $(".item").each(function () {
//     //   const job = {};
//     //   job.title = $(this).find(".title .title-text").text();
//     //   job.salary = $(this).find(".title .salary").text();
//     //   job.companyName = $(this).find(".name .company").text();
//     //   job.workplace = $(this).find(".name .workplace").text();
//     //   const labels = [];
//     //   $(this)
//     //     .find(".labels span")
//     //     .each(function () {
//     //       labels.push($(this).text());
//     //     });
//     //   job.labels = labels.join(",");
//     //   job.recruiterName = $(this).find(".recruiter .user-wrap .name").text();
//     //   job.recruiterAvatar = $(this)
//     //     .find(".recruiter .user-wrap img")
//     //     .attr("src");
//     //   // console.log(job);
//     //   jobs.push(job);
//     // });
//   });
//   console.log(jobs.length);
//   fs.writeFileSync("./jobs.json", JSON.stringify(jobs));
// });

// axios
//   .get(
//     "https://www.zhipin.com/wapi/zpgeek/mobile/search/joblist.json?city=101180100&querySource=1&page=2&city=&query=",
//     {
//       headers: {
//         cookie: `wd_guid=6703a7f0-34e5-4d16-8028-4fd933795e67; historyState=state; __g=-; _bl_uid=I3l51lUvd9C53sna7fmjhhC4Onz7; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1691649585,1691651303,1691651701,1692146685; lastCity=101180100; __fid=1348f8915661c0d4a1a8cf3d10b9eab8; __zp_stoken__=94edeWAVIGmkOahI8XnghEA1MYCgtPDRVAyMZfHgISgMLVhcAOkATIgpRYilyBic0R1d%2BQAB2NnsgJHoALwEsNnNLQWEqDHYqRwZYTxloZXVnMDpgHRokR2M%2FWRQvNhF%2FBW5MP31WNFg2YXQ%3D; __l=l=%2Fwww.zhipin.com%2Fweb%2Fgeek%2Fjob%3Fquery%3D%25E4%25B8%25BB%25E6%2592%25AD%26city%3D101180100&s=3&friend_source=0&s=3&friend_source=0; __c=1692155427; __a=39254695.1692155427..1692155427.23.1.23.23; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1692164155`,
//         "User-Agent":
//           "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",
//         // Host: "49.89.103.240",
//         // Referer: "49.89.103.240",
//       },
//       // proxy: {
//       //   host: "223.215.177.103",
//       //   port: "8089",
//       // },
//     }
//   )
//   .then((res) => {
//     if (res.data.zpData.html) {
//       const $ = cheerio.load(res.data.zpData.html);
//       $(".item").each(function () {
//         const job = {};
//         job.title = $(this).find(".title .title-text").text();
//         job.salary = $(this).find(".title .salary").text();
//         job.companyName = $(this).find(".name .company").text();
//         job.workplace = $(this).find(".name .workplace").text();
//         const labels = [];
//         $(this)
//           .find(".labels span")
//           .each(function () {
//             labels.push($(this).text());
//           });
//         job.labels = labels.join(",");
//         job.recruiterName = $(this).find(".recruiter .user-wrap .name").text();
//         job.recruiterAvatar = $(this)
//           .find(".recruiter .user-wrap img")
//           .attr("src");
//         console.log(job);
//       });
//     } else {
//       console.log(res.data);
//     }
//   });

// test小程序

axios
  .get(
    'https://www.zhipin.com/wapi/zpgeek/miniapp/search/joblist.json?city=101180100&querySource=1&page=2&city=&query=',
    {
      headers: {
        Host: `www.zhipin.com`,
        Connection: `keep-alive`,
        'content-type': `application/x-www-form-urlencoded`,
        zpAppId: `10001`,
        ua: `{"model":"iPhone 12 Pro<iPhone13,3>","platform":"ios"}`,
        platform: `zhipin/windows`,
        ver: `5.1502`,
        scene: `1089`,
        miniappVersion: `5.1502`,
        wt2: `D8jgSLlWKuMMFtxmMI7X2dNtlAuOvJ7ESzMMoPayZbPaMKFSk9FARpNjYk65UUe9krL3QB47cGM0raDkEMtD_sg~~`,
        mpt: `f4b5cea4f5248538d7e8f27f00e01ae1`,
        'x-requested-with': `XMLHttpRequest`,
        // 'X-Forwarded-For': generateProxyIp().ip,
        'Accept-Encoding': `gzip,compress,br,deflate`,
        // 'User-Agent': `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF XWEB/8287`,
        'User-Agent': `Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.40(0x1800282c) NetType/WIFI Language/zh_CN`,
        // Referer: `https://servicewechat.com/wxa8da525af05281f3/461/page-frame.html`,
      },
      // proxy: {
      //   host: "223.215.177.103",
      //   port: "8089",
      // },
    }
  )
  .then((res) => {
    console.log(res.data);
    // if (res.data.zpData.html) {
    //   const $ = cheerio.load(res.data.zpData.html);
    //   $(".item").each(function () {
    //     const job = {};
    //     job.title = $(this).find(".title .title-text").text();
    //     job.salary = $(this).find(".title .salary").text();
    //     job.companyName = $(this).find(".name .company").text();
    //     job.workplace = $(this).find(".name .workplace").text();
    //     const labels = [];
    //     $(this)
    //       .find(".labels span")
    //       .each(function () {
    //         labels.push($(this).text());
    //       });
    //     job.labels = labels.join(",");
    //     job.recruiterName = $(this).find(".recruiter .user-wrap .name").text();
    //     job.recruiterAvatar = $(this)
    //       .find(".recruiter .user-wrap img")
    //       .attr("src");
    //     console.log(job);
    //   });
    // } else {
    //   console.log(res.data);
    // }
  });
